package saleMarket.dao.authority;

import saleMarket.domain.authority.Role;
import util.JdbcHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class RoleDao {
    private static final RoleDao roleDao = new RoleDao();

    private RoleDao() {
    }

    public static RoleDao getInstance() {
        return roleDao;
    }

    /**
     * 根据id，查找各个子类的对象
     *
     * @param connection
     * @param id         目标对象的id
     * @return id对应的目标对象
     * @throws SQLException
     */
    public Role find(Connection connection, int id) throws SQLException {
        //先后在Student和Teacher中查找相应的id
        Role desiredRole = null;
        //编辑预编译语句
        PreparedStatement find_pstmt = connection.prepareStatement
                ("SELECT * FROM role WHERE id = ?");
        //设置预编译参数
        find_pstmt.setInt(1,id);
        //执行查询并获取查询结果
        ResultSet find_resultSet = find_pstmt.executeQuery();
        //赋值
        if (find_resultSet.next()){
            String description = find_resultSet.getString("description");

            //创建相应的顾客对象
            desiredRole = new Role(id,description);
        }
        //释放资源
        JdbcHelper.close(find_resultSet,find_pstmt);
        //返回查询结果
        return desiredRole;
    }

}
